package com.pureut.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.pureut.order.dto.CustomerSaleRankDto;
import com.pureut.order.dto.PurchaseOrderDto;
import com.pureut.order.dto.PurchaseOrderTraceDto;
import com.pureut.order.service.PurchaseOrderService;
import com.pureut.order.vo.OrderReviewVo;
import com.pureut.order.vo.PurchaseOrderVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.order.api.vo.PurchaseOrderTraceVo;
import com.sale.order.api.vo.UpdatePurchaseOrderDataVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 采购订单控制层
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/purchase-order")
public class PurchaseOrderController extends BaseController {


    private final PurchaseOrderService purchaseOrderService;


    /**
     * 订单列表
     */
    @GetMapping("/list")
    //@SaCheckPermission("order:purchase:order:list")
    public TableDataInfo<Map<String,Object>> list(PurchaseOrderVo purchaseOrderVo, PageQuery pageQuery) {
        //查询供应商列表
        return purchaseOrderService.getList(purchaseOrderVo, pageQuery);
    }

    /**
     * 采购订单新增
     */
    @PostMapping("/add")
    //@SaCheckPermission("order:purchase:order:add")
    public R<Void> add(@RequestBody Map<String,Object> map) throws Exception {
        return toAjax(purchaseOrderService.addOrder(map));
    }

    /**
     * 新增（采购计划调用）
     */
    @PostMapping("/planConfirmation")
    public R<Void> planConfirmation(@RequestBody PurchaseOrderVo purchaseOrderVo) throws Exception {
        return toAjax(purchaseOrderService.planConfirmation(purchaseOrderVo));
    }

    /**
     * 删除
     */
    @DeleteMapping("/deleteDocument/{ids}")
    @Log(title = "采购订单删除", businessType = BusinessType.DELETE)
    //@SaCheckPermission("order:purchase:order:remove")
    public R<Void> deleteDocument(@PathVariable("ids") String ids) {
        return toAjax(purchaseOrderService.deleteDocument(ids));
    }

    /**
     * 采购下单
     */
    //@SaCheckPermission("order:purchase:order:placeOrder")
    @Log(title = "采购下单", businessType = BusinessType.UPDATE)
    @PostMapping("/placeOrder")
    public R<Void> placeOrder(@Validated @RequestBody PurchaseOrderVo purchaseOrderVo) {
        return toAjax(purchaseOrderService.updateOrder(purchaseOrderVo));
    }

    /**
     * 提交审核
     */
    //@SaCheckPermission("order:purchase:order:subExamine")
    @Log(title = "提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitReview/{id}")
    public R<Void> submitReview(@PathVariable("id") Long id) {
        return toAjax(purchaseOrderService.submitReview(id));
    }

    /**
     * 撤销审核
     */
    //@SaCheckPermission("order:purchase:order:revoke")
    @Log(title = "撤销审核", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteReview/{id}")
    public R<Void> deleteReview(@PathVariable("id") Long id) {
        return toAjax(purchaseOrderService.deleteReview(id));
    }

    /**
     * 关结
     */
    //@SaCheckPermission("order:purchase:order:closing")
    @Log(title = "关结", businessType = BusinessType.UPDATE)
    @PostMapping("/closeReview")
    public R<Void> closeReview(@RequestBody PurchaseOrderVo purchaseOrderVo) {
        return toAjax(purchaseOrderService.closeReview(purchaseOrderVo));
    }

    /**
     * 修改
     */
    //@SaCheckPermission("order:purchase:order:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateOrder")
    public R<Void> updateOrder(@RequestBody Map<String,Object> map) throws JsonProcessingException {
        return toAjax(purchaseOrderService.updateOrderById(map));
    }

    /**
     * 查看订单信息
     */
    //@SaCheckPermission("order:purchase:order:query")
    @GetMapping("/view/{id}")
    public R<Map<String,Object>> view(@PathVariable("id") Long id) {
        return R.ok(purchaseOrderService.getViewById(id));
    }

    /**
     * 采购入库
     */
    //@SaCheckPermission("order:purchase:order:planRelease")
    @Log(title = "采购入库", businessType = BusinessType.INSERT)
    @PostMapping("/batchArrival")
    public R<Void> batchArrival(@RequestBody PurchaseOrderDto purchaseOrderDto) throws Exception {
        return toAjax(purchaseOrderService.batchArrival(purchaseOrderDto));
    }

    /**
     * 审核单据
     */
    //@SaCheckPermission("order:purchase:order:review")
    @Log(title = "审核单据", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseOrderReview")
    public R<Void> purchaseOrderReview(@Validated @RequestBody OrderReviewVo orderReviewVo) throws Exception {
        return toAjax(purchaseOrderService.purchaseOrderReview(orderReviewVo));
    }

    /**
     * 修改采购订单数据（dubbo调用）
     */
    @PostMapping("/updateOrderData")
    public R<Void> updateOrderData(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return toAjax(purchaseOrderService.updateOrderData(updatePurchaseOrderDataVo));
    }

    /**
     * 修改采购订单数据（dubbo调用）扣减重量
     */
    @PostMapping("/updateOrderDataSub")
    public R<Void> updateOrderDataSub(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return toAjax(purchaseOrderService.updateOrderDataSub(updatePurchaseOrderDataVo));
    }

    /**
     * 修改采购订单退货重量（累加）Feign
     */
    @PostMapping("/updateSalesReturn")
    public boolean updateSalesReturn(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return purchaseOrderService.updateSalesReturn(updatePurchaseOrderDataVo);
    }

    /**
     * 修改采购订单退货重量（扣除）Feign
     */
    @PostMapping("/updateDeductSalesReturn")
    public boolean updateDeductSalesReturn(@RequestBody UpdatePurchaseOrderDataVo updatePurchaseOrderDataVo) {
        return purchaseOrderService.updateDeductSalesReturn(updatePurchaseOrderDataVo);
    }

    /**
     * 获取审核过后的（待到货、部分到货）采购订单数据
     */
    @GetMapping("/passList")
    public TableDataInfo<PurchaseOrderDto> passList(PurchaseOrderVo purchaseOrderVo, PageQuery pageQuery) {
        //查询供应商列表
        return purchaseOrderService.passList(purchaseOrderVo, pageQuery);
    }


    /**
     * 采购订单数(首页工作台使用)
     */
    @GetMapping("/getPurchaseOrderNum")
    public String getPurchaseOrderNum() {
        return purchaseOrderService.getPurchaseOrderNum();
    }

    /**
     * 采购订单金额（首页工作台使用）
     */
    @GetMapping("/getPurchaseOrderPrice")
    public String getSaleOrderPrice() {
        return purchaseOrderService.getPurchaseOrderPrice();
    }

    /**
     * 前五大供应商采购占比(元)
     */
    @GetMapping("/supplierSaleRank")
    public R<List<CustomerSaleRankDto>> supplierSaleRank() {
        return R.ok(purchaseOrderService.supplierSaleRank());
    }

    /**
     * 采购订单追踪列表
     */
    @GetMapping("/purchaseOrderTrace")
    public TableDataInfo<PurchaseOrderTraceDto> purchaseOrderTrace(PurchaseOrderTraceVo purchaseOrderTraceVo,PageQuery pageQuery) {
        return purchaseOrderService.purchaseOrderTrace(purchaseOrderTraceVo, pageQuery);
    }

    /**
     * 采购付款一览表获取（采购订单号，）
     */
}
